#### "How do past repression and indoctrination affect redistributive preferences?" ####
# authors: "Pelke, Lars"
# date: 2019-10-23
# written under "R version 3.6.0 (2019-03-11)"

#### Preliminaries ####

R.version$version.string

# clear workspace
rm(list=ls())

# set working directory

# loading packages

library(countrycode)
library(tidyverse)
library(viridis)
library(scales)
library(readstata13)

#### Import Data ####

#Load World value survey data

issp1987 <- read.dta13("data/ISSP/1987/ZA1680.dta")
issp1992 <- read.dta13("data/ISSP/1992/ZA2310.dta")
issp1999 <- read.dta13("data/ISSP/1999/ZA3430.dta")
issp2009 <- read.dta13("data/ISSP/2009/ZA5400_v4-0-0.dta")


#### Reduce Datasets ####

# ISSP 1987#
issp1987<- issp1987 %>%
  rename(country_code = v3, 
         sex = v83, 
         age = v82, 
         education = v88, 
         employment_status = v74,
         income_equality = v48, 
         government_resp = v49) 

table(issp1987$v3)
issp1987 <- issp1987 %>%
  mutate(country_name = case_when(country_code=="aus"~ "Australia", 
                                  country_code=="d"~ "Germany", 
                                  country_code=="gb"~ "Great Britain",
                                  country_code=="usa"~ "United States of America", 
                                  country_code=="a"~ "Austria", 
                                  country_code=="h"~ "Hungary", 
                                  country_code=="nl"~ "Netherlands", 
                                  country_code=="i"~ "Italy", 
                                  country_code=="ch"~ "Switzerland", 
                                  country_code=="pl"~ "Poland")) 
issp1987$iso3n <- countrycode(issp1987$country_name, "country.name", "iso3n", warn = TRUE)
issp1987$year <- 1987
issp1987$wave <- "ISSP 1987"
issp1987$data <- "ISSP"

issp1987 <- issp1987 %>%
  select(data, iso3n, year, wave, sex, age, education, employment_status, income_equality, government_resp)

table(issp1987$sex)
issp1987 <- issp1987 %>%
  mutate(sex = ifelse(sex=="male", 0, 
                      ifelse(sex=="female", 1, NA)))

table(issp1987$education)

table(issp1987$employment_status)
issp1987 <- issp1987 %>%
  mutate(unemployed = ifelse(employment_status == "unemployed", 1, 0))
table(issp1987$unemployed)


table(issp1987$income_equality)
issp1987 <- issp1987 %>%
  mutate(income_equality = case_when(income_equality=="STRONGLY AGREE" ~ 5, 
                                     income_equality=="agree" ~ 4, 
                                     income_equality=="NEITHER AGREE NOR DIS" ~ 3, 
                                     income_equality=="disagree" ~ 2, 
                                     income_equality=="STRONGLY DISAGREE" ~ 1))
summary(issp1987$income_equality)

           
table(issp1987$government_resp)
issp1987 <- issp1987 %>%
  mutate(government_resp = case_when(government_resp=="STRONGLY AGREE" ~ 5, 
                                     government_resp=="agree" ~ 4, 
                                     government_resp=="NEITHER AGREE NOR DIS" ~ 3, 
                                     government_resp=="disagree" ~ 2, 
                                     government_resp=="STRONGLY DISAGREE" ~ 1))
summary(issp1987$government_resp)


# ISSP 1992#

issp1992<- issp1992 %>%
  rename(country_code = v3, 
         sex = v99, 
         age = v100, 
         education = v103, 
         employment_status = v104, 
         income_equality = v56, 
         government_resp = v57) 

table(issp1992$country_code)
issp1992 <- issp1992 %>%
  mutate(country_name = case_when(country_code=="aus"~ "Australia", 
                                  country_code=="D-W"~ "West Germany", 
                                  country_code=="D-E"~ "East Germany", 
                                  country_code=="gb"~ "Great Britain",
                                  country_code=="usa"~ "United States of America", 
                                  country_code=="a"~ "Austria", 
                                  country_code=="h"~ "Hungary", 
                                  country_code=="n"~ "Norway", 
                                  country_code=="i"~ "Italy", 
                                  country_code=="s"~ "Sweden",
                                  country_code=="cz"~ "Czechoslovakia", 
                                  country_code=="slo"~ "Slovenia", 
                                  country_code=="pl"~ "Poland", 
                                  country_code=="bg"~ "Bulgaria", 
                                  country_code=="rus"~ "Russia", 
                                  country_code=="nz"~ "New Zealand",
                                  country_code=="cdn"~ "Canada", 
                                  country_code=="rp"~ "Philippines")) 

issp1992$iso3n <- countrycode(issp1992$country_name, "country.name", "iso3n", warn = TRUE)

issp1992$iso3n[issp1992$country_name == "East Germany"] <- 278
issp1992$iso3n[issp1992$country_name == "Czechoslovakia"] <- 200

issp1992$year <- 1992
issp1992$wave <- "ISSP 1992"
issp1992$data <- "ISSP"

issp1992 <- issp1992 %>%
  select(data, iso3n, year, wave, sex, age, education, employment_status, income_equality, government_resp)

table(issp1992$sex)
issp1992 <- issp1992 %>%
  mutate(sex = ifelse(sex=="Male", 0, 
                      ifelse(sex=="Female", 1, NA)))

table(issp1992$education)
issp1992 <- issp1992 %>%
  mutate(education = case_when(education == "None" ~ 1, 
                               education == "Incomplete primary" ~ 2,
                               education == "Primary completed" ~ 3,
                               education == "Incomplete secondary" ~ 4,
                               education == "Secondary completed" ~ 5,
                               education == "incomp semi-higher" ~ 6,
                               education == "comp semi-higher" ~ 7,
                               education == "Incomplete university" ~ 8,
                               education == "University completed" ~ 9), 
         education = ifelse(education == "na", NA, education))

table(issp1992$employment_status)
issp1992 <- issp1992 %>%
  mutate(unemployed = ifelse(employment_status == "Unemployed", 1, 0))
table(issp1992$unemployed)

table(issp1992$income_equality)
issp1992 <- issp1992 %>%
  mutate(income_equality = case_when(income_equality=="Strongly agree" ~ 5, 
                                     income_equality=="Agree" ~ 4, 
                                     income_equality=="Neither agree or disagree" ~ 3, 
                                     income_equality=="Disagree" ~ 2, 
                                     income_equality=="Strongly disagree" ~ 1))
summary(issp1992$income_equality)


table(issp1992$government_resp)
issp1992 <- issp1992 %>%
  mutate(government_resp = case_when(government_resp=="Strongly agree" ~ 5, 
                                     government_resp=="Agree" ~ 4, 
                                     government_resp=="Neither agree nor disagree" ~ 3, 
                                     government_resp=="Disagree" ~ 2, 
                                     government_resp=="Strongly disagree" ~ 1))
summary(issp1992$government_resp)

# ISSP 1999#

issp1999<- issp1999 %>%
  rename(country_code = v3, 
         education = degree, 
         employment_status = wrkst, 
         income_equality = v34, 
         government_resp = v35) 

table(issp1999$country_code)

issp1999$iso3n <- countrycode(issp1999$country_code, "country.name", "iso3n", warn = TRUE)

issp1999$iso3n[issp1999$country_code == "D-E Germany East"] <- 278
issp1999$iso3n[issp1999$country_code == "NIRL North Ireland"] <- 826

issp1999$year <- 1999
issp1999$wave <- "ISSP 1999"
issp1999$data <- "ISSP"

issp1999 <- issp1999 %>%
  select(data, iso3n, year, wave, sex, age, education, employment_status, income_equality, government_resp, class)

table(issp1999$sex)
issp1999 <- issp1999 %>%
  mutate(sex = ifelse(sex=="Male", 0, 
                      ifelse(sex=="Female", 1, NA)))

table(issp1999$education)
issp1999 <- issp1999 %>%
  mutate(education = case_when(education == "None" ~ 1, 
                               education == "Incpl primary" ~ 2,
                               education == "Primary compl" ~ 3,
                               education == "Incpl secondary" ~ 4,
                               education == "Secondary compl" ~ 5,
                               education == "Incpl university" ~ 8,
                               education == " University compl" ~ 9), 
         education = ifelse(education == "na", NA, 
                            ifelse(education == "Cant choose", NA, education)))

table(issp1999$employment_status)
issp1999 <- issp1999 %>%
  mutate(unemployed = ifelse(employment_status == "Unemployed", 1, 0))
table(issp1999$unemployed)

table(issp1999$class)
issp1999 <- issp1999 %>%
  mutate(class = case_when(class == "Lower class" ~ 1, 
                           class == "Working class" ~ 2,
                           class == "Upper working" ~ 3,
                           class == "Middle" ~ 4,
                           class == "Upper middle" ~ 4,
                           class == "Upper" ~ 5))

table(issp1999$income_equality)
issp1999 <- issp1999 %>%
  mutate(income_equality = case_when(income_equality=="Strongly agree" ~ 5, 
                                     income_equality=="Agree" ~ 4, 
                                     income_equality=="Neither agree nor disagree" ~ 3, 
                                     income_equality=="Disagree" ~ 2, 
                                     income_equality=="Strongly disagree" ~ 1), 
         income_equality = ifelse(income_equality=="Cant choose", NA, 
                                  ifelse(income_equality=="No answer", NA, income_equality)))
summary(issp1999$income_equality)


table(issp1999$government_resp)
issp1999 <- issp1999 %>%
  mutate(government_resp = case_when(government_resp=="Strongly agree" ~ 5, 
                                     government_resp=="Agree" ~ 4, 
                                     government_resp=="Neither agree or disagree" ~ 3, 
                                     government_resp=="Disagree" ~ 2, 
                                     government_resp=="Strongly disagree" ~ 1), 
         government_resp = ifelse(government_resp=="Cant choose", NA, 
                                  ifelse(government_resp=="No answer", NA, government_resp)))
summary(issp1999$government_resp)

# ISSP 2009#

issp2009<- issp2009 %>%
  rename(country_code = V4, 
         education = DEGREE, 
         sex = SEX, 
         age = AGE, 
         employment_status = WRKST, 
         income_equality = V32, 
         government_resp = V33, 
         class = TOPBOT) 

table(issp2009$country_code)

issp2009$iso3n <- countrycode(issp2009$country_code, "country.name", "iso3n", warn = TRUE)

issp2009$iso3n[issp2009$country_code == "DE-E-Germany-East"] <- 278
issp2009$iso3n[issp2009$country_code == "BE-FLA-Flanders"] <- 056

issp2009$year <- 2009
issp2009$wave <- "ISSP 2009"
issp2009$data <- "ISSP"

issp2009 <- issp2009 %>%
  select(data, iso3n, year, wave, sex, age, education, employment_status, income_equality, government_resp, class)

table(issp2009$sex)

issp2009 <- issp2009 %>%
  mutate(sex = ifelse(sex=="Male", 0, 
                       ifelse(sex=="Female", 1, NA)))

table(issp2009$age)
issp2009 <- issp2009 %>%
  mutate(age = ifelse(age=="98 years or more", 98, 
                      ifelse(sex=="NA, refused", NA, age)))
issp2009$age <- as.integer(issp2009$age)
class(issp2009$age)

table(issp2009$education)
issp2009 <- issp2009 %>%
  mutate(education = case_when(education == "No formal qualification " ~ 1, 
                               education == "Above lowest qualification" ~ 2,
                               education == "Lowest formal qualification" ~ 3,
                               education == "Higher secondary completed" ~ 5,
                               education == "Above higher secondary level, other qualification" ~ 7,
                               education == "University degree completed" ~ 9), 
         education = ifelse(education == "NA", NA, 
                            ifelse(education == " Dont know", NA, education)))

table(issp2009$employment_status)
issp2009 <- issp2009 %>%
  mutate(unemployed = ifelse(employment_status == "Unemployed", 1, 
                             ifelse(employment_status =="NA", NA, 
                                    ifelse(employment_status =="Dont know", NA, 
                                           ifelse(employment_status =="Refused", NA, 0)))))
table(issp2009$unemployed)

table(issp2009$class)
issp2009 <- issp2009 %>%
  mutate(class = case_when(class == "Bottom, Lowest, 01" ~ 1, 
                           class == "02" ~ 1,
                           class == "03" ~ 2,
                           class == "04" ~ 2,
                           class == "05" ~ 3,
                           class == "06" ~ 4,
                           class == "07" ~ 4,
                           class == "08" ~ 4,
                           class == "09" ~ 5,
                           class == "Top, Highest, 10" ~ 5), 
         class = ifelse(class=="NA", NA, ifelse(class=="Dont know",NA, ifelse(class=="Refused", NA, class))))

table(issp2009$income_equality)
issp2009 <- issp2009 %>%
  mutate(income_equality = case_when(income_equality=="Strongly agree" ~ 5, 
                                     income_equality=="Agree" ~ 4, 
                                     income_equality=="Neither agree nor disagree" ~ 3, 
                                     income_equality=="Disagree" ~ 2, 
                                     income_equality=="Strongly disagree" ~ 1), 
         income_equality = ifelse(income_equality=="Cant choose", NA, 
                                  ifelse(income_equality=="NA", NA, income_equality)))
summary(issp2009$income_equality)


table(issp2009$government_resp)
issp2009 <- issp2009 %>%
  mutate(government_resp = case_when(government_resp=="Strongly agree" ~ 5, 
                                     government_resp=="Agree" ~ 4, 
                                     government_resp=="Neither agree or disagree" ~ 3, 
                                     government_resp=="Disagree" ~ 2, 
                                     government_resp=="Strongly disagree" ~ 1), 
         government_resp = ifelse(government_resp=="Cant choose", NA, 
                                  ifelse(government_resp=="NA", NA, government_resp)))
summary(issp2009$government_resp)

#### Merge different ISSP waves ####

issp <- issp1987 %>%
  bind_rows(issp1992, issp1999, issp2009)

issp <- issp %>%
  select(-employment_status)

#### Prepare ISSP Variables for Empirical Analysis ####

issp <- issp %>%
  mutate(education_3 = ifelse(education<=3, 1, 
                              ifelse(education>4 & education <= 7, 2, 3)))
summary(issp$education_3)

issp <- issp %>%
  mutate(birth_year = year-age)
summary(issp$birth_year)
sum(is.na(issp$birth_year))

issp <- issp %>%
  mutate(social_class = class) 
summary(issp$social_class)

#### Age and Cohorts WVS ####

## cohort of respondents ##

issp <- issp %>%
  mutate(cohort = birth_year)
summary(issp$birth_year)


issp <- issp %>%
  mutate(cohort_5 = cut(issp$cohort, seq(1885, 2020, by = 5), right = F, labels = c(1885, 1890, 1895, 1900, 1905,
                                                                                       1910, 1915, 1920, 1925, 1930, 
                                                                                       1935, 1940, 1945, 1950, 1955,
                                                                                       1960, 1965, 1970, 1975, 1980,
                                                                                       1985, 1990, 1995, 2000, 2005, 
                                                                                       2010, 2015)
  )) 

issp$cohort_5 <- as.numeric(as.character(issp$cohort_5))

issp <- issp %>%
  mutate(cohortmatch5_15 = cohort_5 + 15) # 5-year cohorts year plus 15 years" socialization years

issp <- issp %>%
  mutate(cohortmatch5_20 = cohort_5 + 20) # 5-year cohorts year plus 20 years of socialization

table(issp$cohort_5)
table(issp$cohortmatch5_15)
table(issp$cohortmatch5_20)


#################################################################################################################
#################################################################################################################

#### RECODING OF VARIABLES FOR HARMONIZATION OF DATASETS ####

# Sex: no recording nessevary 0: men, 1: women

# Social Class: from 1 (low) to 5 (high/upper)

# Education category: into 3-three scale: done above at education_3

# unemployed: already done (binary scale)

#### Rescale Dependent Variables ####

# income_equality

table(issp$income_equality)

issp$income_equality <- rescale(issp$income_equality, to = c(0, 100))
table(issp$income_equality)


# government_resp 

table(issp$government_resp)
issp$government_resp <- rescale(issp$government_resp, to = c(0, 100))
table(issp$government_resp)

#### SAVE DATASET ####

issp <- issp %>%
  dplyr::select(data, iso3n, year, wave, sex, age, education, education_3, birth_year, 
                social_class, unemployed, income_equality, government_resp, 
                cohort_5, cohortmatch5_15, cohortmatch5_20)

saveRDS(issp, file = "data/issp_prepared.rds")
